package com.tust.androidreader;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.widget.ScrollView;

/**********************已经用onTouch方法替代上拉、下拉加载功能***********************/
public class ScrollLoadScrollView extends ScrollView {//实现上拉、下拉加载的功能
    private  OnScrollBottomListener bottomListener;
    /*private  OnScrollTopListener topListener;*/
    private int calCount;

    public interface OnScrollBottomListener {
        void scrollToBottom();
    }

    public void onScrollViewScrollToBottom (OnScrollBottomListener onScrollBottomListener) {
        bottomListener = onScrollBottomListener;
    }

    public void unRegisterOnScrollViewScrollToBottom() {
        bottomListener = null;
    }

   /* public interface OnScrollTopListener {
        void scrollToTop();
    }

    public void onScrollViewScrollToTop (OnScrollTopListener onScrollTopListener) {
        topListener = onScrollTopListener;
    }

    public void unRegisterOnScrollViewScrollToTop() {
        topListener = null;
    }*/

    public ScrollLoadScrollView(Context context) {
        super(context);
    }

    public ScrollLoadScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScrollLoadScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ScrollLoadScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        View view = this.getChildAt(0);
        if (this.getHeight() + this.getScrollY() == view.getHeight()) {
            calCount++;
            if (calCount == 1) {
                if (bottomListener != null) {
                    bottomListener.scrollToBottom();
                }
            }
        /*} else if (this.getHeight() - this.getScrollY() == view.getHeight()) {//上拉加载
            calCount++;
            if (calCount == 1) {
                if (topListener != null) {
                    topListener.scrollToTop();
                }
            }*/
        } else {
            calCount = 0;
        }
    }
}
